import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:umeng_vertify/generated/json/base/json_convert_content.dart';
import 'package:umeng_vertify/result_data_entity.dart';
import 'package:umeng_vertify/result_value.dart';
import 'package:umeng_vertify/u_m_token_entity.dart';

export 'result_data_entity.dart';

class UmengVertify {
  static const MethodChannel _channel = const MethodChannel('umeng_vertify');
  static const MethodChannel _iosModelchannel =
      const MethodChannel('yyt.verify.model');
  static const EventChannel event = const EventChannel("umeng_vertify_event");

  static getData(Map jsonMap,
      {ValueChanged<UMCallBackData>? umTokenDataGet,
      ValueChanged<String>? error}) {
    switch (jsonMap["errorCode"]) {
      case 0:
        print("数据呢铁子0${jsonMap["resultTokenData"]}");
        break;
      case 1:
        if (!jsonMap.containsKey("uMTokenData")) return;
        String dataJson = jsonMap["uMTokenData"];
        UMTokenData data = JsonConvert.fromJsonAsT<UMTokenData>(json.decode(dataJson));

        if(Platform.isIOS) {
          ResultValue? resultValue = ResultValue.checkValue(int.parse(data.resultCode));
          UMCallBackData callBackData = UMCallBackData(data, resultValue);
          umTokenDataGet?.call(callBackData);

        }else{
          ResultValue? resultValue = ResultValue.checkValue(int.parse(data.code));
          UMCallBackData callBackData = UMCallBackData(data, resultValue);

          umTokenDataGet?.call(callBackData);

        }
        break;
      default:
        error?.call(jsonMap["errorMessage"]);
        break;
    }
  }

  static addListener(Future<dynamic> Function(MethodCall call)? handler) {
    _channel.setMethodCallHandler(handler);
  }

  static Future initHelper() async {
    return _doMethod(MethodName.initHelper);
  }

  static Future setAuthSDKInfo(String secret) async {
    return _doMethod(MethodName.setAuthSDKInfo, params: {"secret": secret});
  }

  static Future checkEnvAvailable({int type = 1}) async {
    return _doMethod(MethodName.checkEnvAvailable, params: {"type": type});
  }

  static Future setLoggerEnable({bool showLog = false}) async {
    return _doMethod(MethodName.setLoggerEnable, params: {"isDebug": showLog});
  }

  static Future getVerifyToken({int timeOut = 5000}) async {
    return _doMethod(MethodName.getVerifyToken, params: {"timeOut": timeOut});
  }

  static Future accelerateLoginPage({int timeOut = 5000}) async {
    return _doMethod(MethodName.accelerateLoginPage,
        params: {"timeOut": timeOut});
  }

  static Future getLoginToken({int timeOut = 5000}) async {
    return _doMethod(MethodName.getLoginToken, params: {"timeOut": timeOut});
  }

  static Future quitLoginPage() async {
    return _doMethod(MethodName.quitLoginPage);
  }

  static Future hideLoginLoading() async {
    return _doMethod(MethodName.hideLoginLoading);
  }

  static Future addAuthRegisterXmlConfig() async {
    return _doMethod(MethodName.addAuthRegisterXmlConfig);
  }

  static Future loginField() async {
    return _doMethod(MethodName.loginField);
  }

  static Future addAuthRegistViewConfig() async {
    return _doMethod(MethodName.addAuthRegistViewConfig);
  }

  static Future showCloseButton() async {
    return _doMethod(MethodName.showCloseButton);
  }

  static Future showLoading() async {

    return _doMethod(MethodName.showLoading);
  }

  static Future getIOSVerifyViewModel() async {
    return _doiOSModelMethod(MethodName.getIOSVerifyViewModel);
  }

  static Future _doMethod(String name, {Map? params}) async {
    return await _channel.invokeMethod(name, params ?? {});
  }

  static Future _doiOSModelMethod(String name, {Map? params}) async {
    if (Platform.isIOS){
      return await _iosModelchannel.invokeMethod(name, params ?? {});
    }else {
      return ;
    }
  }
}

class MethodName {
  static String initHelper = "initVerify";

  static String setAuthSDKInfo = "setAuthSDKInfo";

  static String checkEnvAvailable = "checkEnvAvailable";

  static String setLoggerEnable = "setLoggerEnable";

  static String getVerifyToken = "getVerifyToken";

  static String accelerateLoginPage = "accelerateLoginPage"; //一键登录预取号
  static String getLoginToken = "getLoginToken"; //一键登录预取号
  static String quitLoginPage = "quitLoginPage"; //退出授权页
  static String hideLoginLoading = "hideLoginLoading"; //退出授权页
  static String addAuthRegisterXmlConfig =
      "addAuthRegisterXmlConfig"; //一键登录页面添加由XML的自定义控件
  static String addAuthRegistViewConfig =
      "addAuthRegistViewConfig"; //一键登录添加自定义控件
  static String getIOSVerifyViewModel = "configVerifyPageModel"; //一键登录添加自定义控件
  static String loginField = "loginField"; //自定义登录失败
  static String showCloseButton = "showCloseButton"; //显示关闭按钮
  static String showLoading = "showLoading"; //显示加载

}
